package com.woniuxy.info.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.info.sys.entity.Informations;
import com.woniuxy.info.sys.mapper.InformationsMapper;
import com.woniuxy.info.sys.page.Pages;
import com.woniuxy.info.sys.service.IInformationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lzj
 * @since 2023-11-16
 */
@Service
public class InformationsServiceImpl extends ServiceImpl<InformationsMapper, Informations> implements IInformationsService {

    @Autowired
    private InformationsMapper informationsMapper;

    /**
     * 分页查询资讯列表
     */
    @Override
    public Pages<Informations> findByPage(Integer current, Integer size) {
        LambdaQueryWrapper<Informations> wrapper = Wrappers.lambdaQuery(Informations.class);
        wrapper.orderByDesc(Informations::getLastPostTime);
        Page<Informations> page = this.page(new Page<>(current, size), wrapper);
        List<Informations> list = page.getRecords();

        Pages<Informations> pageVo = new Pages<>();
        pageVo.setList(list);
        pageVo.setPages(page.getTotal());
        pageVo.setCurrent(current);
        pageVo.setSize(size);

        return pageVo;
    }

    /**
     * 资讯详情
     */
    @Override
    public Informations findInfoById(Integer id) {
        Informations informations = informationsMapper.selectById(id);
        this.update(Wrappers.lambdaUpdate(Informations.class)
                .set(Informations::getViewCount,informations.getViewCount()+1).eq(Informations::getId,id));
        return informations;
    }

    /**
     * 修改资讯评论数量
     */
    @Override
    public Integer updateReplyCount(Integer infoId) {
        Informations informations = this.getById(infoId);
        this.update(Wrappers.lambdaUpdate(Informations.class)
                .set(Informations::getReplyCount, informations.getReplyCount()+1)
                .set(Informations::getLastPostTime, new Date())
                .eq(Informations::getId, infoId));
        return informations.getViewCount();
    }
}
